<?php

namespace app\command;

use app\common\helper\FormatHelper;
use app\common\helper\LogHelper;
use app\common\service\VoiceService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Voice extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('voice');
        // 设置参数
        $this->setDescription('语音播报');
        $this->addArgument('type'); //必传参数

    }

    protected function execute(Input $input, Output $output)
    {

        $type = $input->getArgument('type');
        switch ($type) {
            case 'water':
                $this->_water();// php think voice water
                break;
            case 'voice':
                $this->_voice();// php think voice voice
                break;
            case 'clock':
                $this->_clock();// php think voice clock
                break;
        }
    }


    /**
     * 水吧 商品播报
     */
    protected function _water()
    {
        while (true) {

            sleep(1);

            $water_config = Db::name('qt_water_config')->select();
            foreach ($water_config as $config_key => $config) {

                LogHelper::ConsoleLog('执行门店：' . $config['store_id']);

                $config['play_number'] = !empty($config['play_number']) ? $config['play_number'] : 2;//播放次数
                $order_item = Db::name('qt_product_order_item')
                    ->alias('oi')
                    ->field([
                        'oi.id',
                        'oi.goods_name',
                        'oi.goods_num',
                        'oi.goods_total_price',
                        'po.room_id',
                        'r.name as room_name',
                        'po.engineer_id',
                    ])
                    ->leftJoin('qt_product_order po', 'po.id=oi.order_id')
                    ->leftJoin('qt_room r', 'po.room_id=r.id')
                    ->where('oi.store_id', $config['store_id'])
                    ->where('oi.is_broadcast', 2)//未播报
                    ->order('oi.create_time asc')
                    ->select();

                //设置不接受房间，不播报
                $noreceive_room = explode(',', $config['noreceive_room']);

                LogHelper::ConsoleLog('正在生成下单播报');

                //下单播报
                foreach ($order_item as $key => $val) {

                    if (!empty($noreceive_room) && in_array($val['room_id'], $noreceive_room)) {
                        Db::name('qt_product_order_item')->where('id', $val['id'])->update(['is_broadcast' => 3]);
                        continue;
                    }

                    $text = '房间  ' . $val['room_name'] . '    ' . $val['goods_name'] . '    数量   ' . $val['goods_num'];
                    $result = VoiceService::createVoice($text, $config['play_speed'], $config['play_number']);
                    if ($result['code'] == FormatHelper::SUCCESS) {

                        //合成语音信息
                        VoiceService::saveVoiceMsg($config['store_id'], $text, $result['data'], 1, 1);

                        //更新未播报
                        Db::name('qt_product_order_item')->where('id', $val['id'])->update(['is_broadcast' => 1]);
                    }
                }
                LogHelper::ConsoleLog('下单播报处理完成');

                LogHelper::ConsoleLog('正在生成商品退单播报');
                //商品退单播报
                $send_voice_msg = Db::name('qt_send_voice_msg')
                    ->where('type', 1)
                    ->where('status', 2)
                    ->where('is_create_voice', 2)
                    ->select();
                foreach ($send_voice_msg as $key => $val) {
                    $result = VoiceService::createVoice($val['content'], $config['play_speed'], $config['play_number']);
                    if ($result['code'] == FormatHelper::SUCCESS) {
                        VoiceService::updateVoiceMsg($val['id'], $result['data']);
                    }
                }

                LogHelper::ConsoleLog('商品退单播报处理完成');
            }
            LogHelper::ConsoleLog('_water脚本执行完成');
        }
    }


    /**
     * 语音大屏 项目播报
     */
    protected function _voice()
    {
        while (true) {
            sleep(1);

            $water_config = Db::name('qt_water_config')->select();
            foreach ($water_config as $config_key => $config) {

                LogHelper::ConsoleLog('执行门店：' . $config['store_id']);

                $voice_config = Db::name('qt_voice_config')->where('store_id', $config['store_id'])->find();


                LogHelper::ConsoleLog('处理退单播报');
                //退单播报
                $send_voice_msg = Db::name('qt_send_voice_msg')
                    ->where('type', 2)
                    ->where('status', 2)
                    ->where('is_create_voice',2)
                    ->select();

                foreach ($send_voice_msg as $key => $val) {
                    $result = VoiceService::createVoice($val['content'], $voice_config['play_speed'], $voice_config['play_number']);
                    if ($result['code'] == FormatHelper::SUCCESS) {
                        //合成语音信息
                        VoiceService::updateVoiceMsg($val['id'], $result['data']);
                    }
                }

                LogHelper::ConsoleLog('处理退单播报结束');


                LogHelper::ConsoleLog('处理下单播报');
                //项目播报
                $order_project = Db::name('qt_order_project')
                    ->alias('op')
                    ->field([
                        'op.id',
                        'op.is_add',
                        'op.name project_name',
                        'op.engineer_id',
                        'r.name as room_name',
                        'op.num',
                        'op.order_type',
                    ])
                    ->leftJoin('qt_room r', 'op.room_id=r.id')
                    ->where('op.store_id', $config['store_id'])
                    ->where('op.is_broadcast', 2)//未播报
                    ->select();
                $order_type = [1 => '轮排', 2 => '点钟', 3 => 'call钟', 4 => '优轮', 5 => '加钟'];
                foreach ($order_project as $p_k => $p_v) {

                    LogHelper::ConsoleLog('正在执行 order_project'.$p_v['id'].'的数据');
                    if (empty($p_v['engineer_id'])) {
                        Db::name('qt_order_project')->where('id', $p_v['id'])->update(['is_broadcast' => 3]);
                        continue;
                    }
                    $engineer_data = Db::name('qt_users')->where('id', $p_v['engineer_id'])->find();
                    if (empty($engineer_data)) {
                        Db::name('qt_order_project')->where('id', $p_v['id'])->update(['is_broadcast' => 3]);
                        continue;
                    }

                    //设置工种不语音播报
                    $type_id_arr = explode(',', $voice_config['noplay_work_type']);
                    if (!empty($type_id_arr) && in_array($engineer_data['type_id'], $type_id_arr)) {
                        Db::name('qt_order_project')->where('id', $p_v['id'])->update(['is_broadcast' => 3]);
                        continue;
                    }
                    $is_add_str = '';
                    if($p_v['is_add'] == '2'){$is_add_str = '加班';}
                    $text = $engineer_data['work_number'] . ' 号   ' . $p_v['room_name'] . ' 房  ' . $p_v['project_name'] . '   ' . $is_add_str . $order_type[$p_v['order_type']];

                    LogHelper::ConsoleLog('正在执行生成语音'.$text);

                    $result = VoiceService::createVoice($text, $voice_config['play_speed'], $voice_config['play_number']);

                    LogHelper::ConsoleLog('语音处理完成');

                    if ($result['code'] == FormatHelper::SUCCESS) {
                        VoiceService::saveVoiceMsg($config['store_id'], $text, $result['data'], 3, 2,$engineer_data['id']);
                        //生成语音合成状态
                        Db::name('qt_order_project')->where('id', $p_v['id'])->update(['is_broadcast' => 1]);
                    }
                }

                LogHelper::ConsoleLog('处理下单播报结束');
            }

            LogHelper::ConsoleLog('_voice脚本处理完毕');
        }
    }


    /**
     * 上钟播报
     */
    protected function _clock()
    {


        echo '_clock';
    }

}
